/**
 * Created by user on 2018/4/13.
 */
import './login.less'
import React, { Component } from 'react'
import { connect } from 'react-redux'
import { Button, Input, Icon, Row, Col ,Form } from 'antd'
import LoginForm from '../../components/login/LoginForm'
import { API } from '../../config'
import { LoginActions } from '../../actions'
import { Request, GlobalAlert } from '../../utils'
import CustomComponent from '../../custom/CustomComponent'
const { toggleLoginLoading, toggleAuth } = LoginActions

class Login extends CustomComponent {
  onSubmit = (data) => {
    const { dispatch } = this.props
    dispatch(toggleLoginLoading(true))
    this.doLoginPromise(data)
  }
  async doLoginPromise(data) {
    const { from: fromPath } = this.props.location.state
    const { dispatch } = this.props
    var rp = await Request.SendRequest(API.DO_LOGIN.URL(), API.DO_LOGIN.METHOD, data)
    dispatch(toggleLoginLoading(false))
    if (rp.status === Request.RequestStatus.SUCCESS) {
      if (rp.data.code === 200) {
        GlobalAlert.success('login succ')
        dispatch(toggleAuth(true))
        this.redirect(fromPath, null)
      } else {
        GlobalAlert.error(rp.data.msg || 'login fail')
      }
    } else {
      GlobalAlert.error(rp.msg)
    }
  }
  render() {
    return(
      <div className="screen">
        <div className="login_inner">
          <h2 className="align_center">登录</h2>
          <Row gutter={8}>
            <Col span={14} offset={5}>
              <LoginForm onSubmit={this.onSubmit}/>
            </Col>
          </Row>
        </div>
      </div>
    )
  }
}
const mapState = (state) => {
  return state
}
export default connect(mapState)(Form.create()(Login))
